<?php
/**
 * Feed Link View Helper.
 * @package halo
 */

hc_core_ClassLoader::load('halo_AbstractExternalLinkViewHelper');


/**
 * Feed Link View Helper.
 *
 * The Feed Link View Helper helps manage the inclusion of external feed files
 * by way of <link /> tags.
 *
 * To include external Feeds from one location (from <head/>, for example), use
 * the {@link inc()} method. This pushes the requested feed URI onto the list of
 * Feed URIs to load by {@link renderIncluded()}.
 *
 * <code>
 * <!-- Push /js/sample.js onto the Javascript include stack. -->
 * <?php $feed->inc('/default.xml', array('title' => 'News', 'type' => 'rss')); ?>
 * </code>
 *
 * All of the external Feed files included in this way can be rendered at
 * by calling {@link renderIncluded()}.
 *
 * <code>
 * <!-- To render all included feeds <link /> tags... -->
 * <?php print $feed->renderIncluded(); ?>
 * </code>
 *
 * To include external Feeds from the current location in the template ("here"),
 * use the {@link inline()} method. This method renders the <link /> tag
 * immediately.
 *
 * <code>
 * <!-- This renders the <link /> tag inline "here" in the template. -->
 * <?php print $feed->inline('/default.xml', array('title' => 'News', 'type' => 'rss')); ?>
 * </code>
 *
 */
class halo_FeedLinkViewHelper extends halo_AbstractExternalLinkViewHelper {

    /**
     * Constructor.
     * @param halo_HttpRequest $httpRequest The request.
     * @param halo_HttpResponse $httpResponse The response.
     */
    public function __construct(halo_HttpRequest $httpRequest, halo_HttpResponse $httpResponse) {
        parent::__construct($httpRequest, $httpResponse);
    }

    /**
     * Render external Feed links.
     *
     * Given the URI and options, creates the appropriate link tag. Options can contain
     * the key <strong>title</strong> and either <strong>flavor</strong> or <strong>type</strong>.
     * If no flavor or type are specified, the default <strong>rss</strong> is used.
     * @param string $uri Sanitized URI
     * @param array $options Options.
     * @return string Rendered link.
     */
    protected function renderExternalLink($uri, $options) {

        $type = null;
        $title = null;

        if ( ! isset($options['type']) ) {
            if ( ! isset($options['flavor']) ) $options['flavor'] = 'rss';
            $type = 'application/' . $options['flavor'] . '+xml';
        } else {
            $type = $options['type'];
        }

        if ( isset($options['title']) ) $title = $options['title'];

        $buffer = '<link rel="alternate" type="' . $type . '" ';
        if ( $title ) {
            $buffer .= 'title="' . $title . '" ';
        }
        $buffer .= 'href="' . $uri . '" />';
        return $buffer;
    }
}
?>
